﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Microsoft.Win32;
using System.IO;
using System.Threading;



namespace sentence
{
    public partial class MainForm : Form
    {
        XmlDocument xmldoc;
        bool SaveDesktop;
    
        public MainForm()
        {
            InitializeComponent();
            //判断自动启动设置
            Thread.Sleep(3 * 1000);
            SentenceUpdater CheckUpdaterHandle = new SentenceUpdater();
            try
            {
                if (File.Exists(Application.StartupPath + @"\config.xml"))
                {
                    xmldoc = new XmlDocument();
                    xmldoc.Load(Application.StartupPath + @"\config.xml");
                    XmlNode node = xmldoc.GetElementsByTagName("Sentence").Item(0);
                    XmlElement e = (XmlElement)node;
                    SentenceUtility.WallPaperDate = e.GetAttribute("WallPaperDate");

                    node = xmldoc.GetElementsByTagName("AutoStart").Item(0);
                    RegistryKey HKLM = Registry.CurrentUser;
                    RegistryKey Run = HKLM.CreateSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\");
                    Object path = Run.GetValue(System.Windows.Forms.Application.ProductName);
                    if (path != null && path.ToString() != "\"" + System.Windows.Forms.Application.ExecutablePath + "\"")
                    {
                        if (node.InnerText.ToString() == "True")
                        {
                            Run.SetValue(System.Windows.Forms.Application.ProductName, "\"" + System.Windows.Forms.Application.ExecutablePath + "\"");
                            HKLM.Close();
                        }
                        else
                        {
                            Run.DeleteValue(System.Windows.Forms.Application.ProductName);
                            HKLM.Close();
                        }
                    }
                    node = xmldoc.GetElementsByTagName("AutoExit").Item(0);
                    if (node.InnerText.ToString() == "True")
                    {
                        Thread Thread_AutoExit = new Thread(new ThreadStart(AutoExitThread));
                        Thread_AutoExit.IsBackground = true;
                        Thread_AutoExit.Start();
                    }
                    node = xmldoc.GetElementsByTagName("Save").Item(0);
                    SaveDesktop = (node.InnerText.ToString() == "True") ? true : false;
                    if (SaveDesktop)
                    {
                        node = xmldoc.GetElementsByTagName("SavePath").Item(0);
                        WallPaperDownloader.SavePath = node.InnerText.ToString();
                    }
                }
                else
                {
                    ConfigForm configForm = new ConfigForm();
                    configForm.Show();
                }

                WallPaperDownloader DownloadHandle = new WallPaperDownloader();
                SentenceNotifyIcon.BalloonTipText = "正在更新壁纸...";
                SentenceNotifyIcon.ShowBalloonTip(1000);
            }
            catch (System.Exception ex)
            {
                SentenceNotifyIcon.BalloonTipText = "软件发生错误，\r\n请发送错误报告以便我们修正此BUG";
                SentenceNotifyIcon.ShowBalloonTip(1000);
                BUGReport reporter = new BUGReport(ex.ToString());
                reporter.Show();
            }
            
        }

        private void AutoExitThread()
        {
            Thread.Sleep(5*60*1000);
            Application.Exit();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConfigForm configForm = new ConfigForm();
            configForm.Show();
        }

        private void 关于SentenceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutForm aboutForm = new AboutForm();
            aboutForm.Show();
        }

        private void 最新壁纸ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SentenceNotifyIcon.BalloonTipText = "壁纸更新已经开始...";
            SentenceNotifyIcon.ShowBalloonTip(1000);
        }

        private void BUG提交ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BUGReport bugreportForm = new BUGReport();
            bugreportForm.Show();
        }

        private void 官方网站ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(SentenceUtility.HomePageUrl); 
        }
    }
}
